package cpu.footprint.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cpu.footprint.R;
import cpu.footprint.api.ApiUrl;
import cpu.footprint.bean.BaseBean;
import cpu.footprint.bean.Notice;
import cpu.footprint.bean.User;
import cpu.footprint.common.BaseActivity;
import cpu.footprint.api.APIService;
import cpu.footprint.api.RespCallBack;
import cpu.footprint.view.recycleView.CommonAdapter;
import cpu.footprint.view.recycleView.RecycleViewDivider;
import cpu.footprint.view.recycleView.ViewHolder;

/**
 * Created by WangJinPing on 2018/1/25.
 */
@ContentView(R.layout.activity_groupnotice)
public class GroupNoticeActivity extends BaseActivity {
    @ViewInject(R.id.title_content)
    private TextView title_content;//title内容
    @ViewInject(R.id.title_menu_text)
    private TextView title_menu_text;//title功能
    @ViewInject(R.id.title_menu_image)
    private ImageView title_menu_image;//title功能
    @ViewInject(R.id.swipe_container)
    private SwipeRefreshLayout swipeRefreshLayout;
    @ViewInject(R.id.recycleView)
    private RecyclerView recyclerView;
    private List<Notice> list = new ArrayList<>();
    private CommonAdapter<Notice> adapter;
    private int groupId = 0;
    private User user = new User();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        user = myApplication.getUser();
        groupId = this.getIntent().getIntExtra("groupId",0);
        initView();
        requestData();
    }

    private void initView() {
        title_content.setText("群公告");
        title_menu_image.setVisibility(View.GONE);
        title_menu_text.setVisibility(View.GONE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL, 10, R.color.colorLightGray));
        adapter = new CommonAdapter<Notice>(mContext,R.layout.item_groupnotice,list) {
            @Override
            public void convert(ViewHolder holder, Notice notice) {
                holder.setText(R.id.txtTitle,notice.getName());
                holder.setText(R.id.txtAuthor,notice.getGroup().getOwner());
                holder.setText(R.id.txtDate,notice.getCreateTime());
                holder.setText(R.id.txtContent, notice.getContent());
            }
        };
        recyclerView.setAdapter(adapter);
        swipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestData();
            }
        });
    }

    private void requestData() {
        swipeRefreshLayout.setRefreshing(true);
        Map<String, String> map = new HashMap<>();
        map.put("mobilePhone", user.getMobilePhone());
        map.put("token", user.getToken());
        map.put("groupId",groupId+"");
        APIService.Post(ApiUrl.URL_LIST_NOTICE, map, new RespCallBack<BaseBean<Notice>>(this) {
            @Override
            public void onSuccess(BaseBean<Notice> result) {
                super.onSuccess(result);
                if (result.success) {
                    list.clear();
                    list.addAll(result.datas);
                    System.out.println("------------------"+result.datas.toString());
                    adapter.notifyDataSetChanged();
                }else{
                    Toast("获取数据失败");
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
    @Event(value = {R.id.title_back})
    private void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back: {
                GroupNoticeActivity.this.finish();
                overridePendingTransition(0,R.anim.fade);
            }
            break;
            default:
                break;
        }
    }
}
